package com.gimi.cloud.jimi.app.consumer.service.order.impl;

import com.gimi.cloud.jimi.app.consumer.feign.OrderFeignService;
import com.gimi.cloud.jimi.app.consumer.service.order.OrderService;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.EntityVerification;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.order.ReplantDTO;
import com.gimi.cloud.jimi.dto.params.OrderParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by shazhiwei on 2018/1/9.
 */
@Service
public class OrderServiceImpl implements OrderService {
    private final OrderFeignService orderFeignService;
    private static Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    public OrderServiceImpl(OrderFeignService orderFeignService) {
        this.orderFeignService = orderFeignService;
    }

    @Override
    public void addOrder(String requestBody, HttpServletResponse response, HttpServletRequest request) throws Exception {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        OrderParams orderParams = JsonUtil.jsonToBean(requestBody, OrderParams.class);
        EntityVerification.checkByGroupsThrowsException(orderParams, OrderParams.AddOrder.class);
        EntityVerification.checkByGroupsThrowsException(orderParams.getOrderExpress(), OrderParams.AddOrder.class);//校验收货地址
        EntityVerification.checkByGroupsThrowsException(orderParams.getOrderGoods(), OrderParams.AddOrder.class);//校验商品
        EntityVerification.checkByGroupsThrowsException(orderParams.getOrderCost(), OrderParams.AddOrder.class);//校验商品费用
        Util.sendResponse(orderFeignService.addOrder(orderParams), response);
    }

    @Override
    public void getDataStatus(String requestBody, HttpServletResponse response, HttpServletRequest request) throws Exception {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        OrderParams orderParams = JsonUtil.jsonToBean(requestBody, OrderParams.class);
        if (StringUtil.isBlank(orderParams.getUserId())) {
            throw new MsgException(ResponseCodeEnum.INVALID, "userId不能为空");
        }
        Util.sendResponse(orderFeignService.getDataStatus(orderParams), response);
    }

    @Override
    public void getOrderList(String requestBody, HttpServletResponse response, HttpServletRequest request) throws Exception {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        OrderParams orderParams = JsonUtil.jsonToBean(requestBody, OrderParams.class);
        if (StringUtil.isBlank(orderParams.getUserId())) {
            throw new MsgException(ResponseCodeEnum.INVALID, "userId不能为空");
        }
        Util.sendResponse(orderFeignService.getOrderList(orderParams), response);
    }

    @Override
    public void getOrderDetail(@RequestBody String requestBody, HttpServletResponse response, HttpServletRequest request)
            throws Exception {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        OrderParams param = JsonUtil.jsonToBean(requestBody, OrderParams.class);
        if (StringUtil.isBlank(param.getoId())) {
            throw new MsgException(ResponseCodeEnum.INVALID, "订单oId不能为空");
        }
        Util.sendResponse(orderFeignService.getOrderDetail(param), response);
    }

    @Override
    public void getReplantDetail(String requestBody, HttpServletResponse response, HttpServletRequest request) throws Exception {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ReplantDTO replantDTO = JsonUtil.jsonToBean(requestBody, ReplantDTO.class);
        EntityVerification.checkByGroupsThrowsException(replantDTO,ReplantDTO.ReplantDetail.class);
        Util.sendResponse(orderFeignService.getReplantDetail(replantDTO), response);
    }

}
